package org.edu.modules.logistics.base.service.impl;

import cn.hutool.core.util.IdUtil;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.feign.FeignCommon;
import org.edu.common.feign.vo.VeBaseAppUser;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.logistics.base.mapper.VeHqUserMapper;
import org.edu.modules.logistics.base.entity.VeHqUser;
import org.edu.modules.logistics.base.dto.VeHqUserDto;
import org.edu.modules.logistics.base.service.VeHqUserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


/**
 * 实现类
 */
@Service
public class VeHqUserServiceImpl extends BaseServiceImpl<VeHqUser, VeHqUserMapper> implements VeHqUserService {


    @Autowired
    private FeignCommon feignCommon;


    public IPage<VeHqUserDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeHqUserDto findDtoById(String id) {
        VeHqUser entity = super.findById(id);
        VeHqUserDto dto = ConvertUtils.convertToDto(entity, VeHqUserDto.class);
        return dto;
    }

    /**
    * 新增或者修改
    */
    @Transactional
    public void insertOrUpdate(VeHqUserDto dto,Boolean isAdd) throws Exception{

        if(isAdd){
            dto.setId(IdUtil.simpleUUID());
            dto.setUserId(dto.getId());
            super.insert(dto);
        }else{
            super.update(dto);
        }
        this.insertBase(dto);

    }

    private void insertBase(VeHqUserDto dto) throws Exception{

        VeBaseAppUser veBaseAppUser  = new VeBaseAppUser();
        veBaseAppUser.setInterfaceUserId("09d5e1e7f9b049008eee645c783a1d67");
               veBaseAppUser.setNewPwd("123456");
                 veBaseAppUser.setOldPwd("123456");
        veBaseAppUser.setRealName(dto.getName());
        veBaseAppUser.setRoleId(17);
        veBaseAppUser.setSex(dto.getSex()+"");
        veBaseAppUser.setUserId(dto.getId()+10000);
        veBaseAppUser.setUserName(dto.getNumber());
        veBaseAppUser.setUserPassword("123456");
        veBaseAppUser.setUserRoleId(17);
        veBaseAppUser.setUserTel(dto.getPhone());
        veBaseAppUser.setUserType("2");//用户类别(1老师 2学生)

        Map map =  feignCommon.addVeBaseAppUser(veBaseAppUser);
        if((Integer) map.get("code")!=200) {
            throw new Exception((String)map.get("message"));
        }
    }

}
